<?php 
require_once 'BaseMySQLDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Entity/Domain/TbluserDomain.php';
class TbluserDAO extends BaseMySQLDAO {

	public function __construct($context) {
		parent::__construct($context, TbluserDomain::$CollectionName);
	}

	public function create($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "INSERT INTO tbluser(userID,userName,pass,lastLogin,isActive,lockOutTime)
					VALUES	(
								".$obj->getUserID().",
								'".$obj->getUserName()."',
								'".$obj->getPass()."',
								".$obj->getLastLogin().",
								".$obj->getIsActive().",
								".$obj->getLockOutTime()."
							)";
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function deleteByOid($oid){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "DELETE FROM tbluser WHERE userID=".$oid;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function update($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE tbluser
				SET
					tbluser.userName = '".$obj->getUserName()."',
					tbluser.pass = '".$obj->getPass()."',
					tbluser.lastLogin = ".$obj->getLastLogin().",
					tbluser.isActive = ".$obj->getIsActive().",
					tbluser.lockOutTime = ".$obj->getLockOutTime()."
				 WHERE userID=".$obj->getUserID();
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function changePass($newpass, $uid) 
	{  
		$this->getContext()->getDBTransaction()->startTransaction();
        $inSQL = "UPDATE tbluser SET tbluser.pass = '".$newpass."' where tbluser.userID = '$uid'";  
		$this->getMySQLConnection()->mysqlData($inSQL);
        $this->getContext()->getDBTransaction()->closeTransaction();     
	}
	public function checkPassExists($pass, $uid) 
	{  
		$this->getContext()->getDBTransaction()->startTransaction();
        $inSQL = "SELECT * FROM tbluser WHERE tbluser.pass = '$pass' and tbluser.userID = '$uid'";  
		$result = $this->getMySQLConnection()->mysqlOneRow($inSQL);
        $this->getContext()->getDBTransaction()->closeTransaction();
        if(!Tool::isEmpty($result))
		{
            return true;  
		}
		else 
			return false;     
	}
	
	public function findByOid($oid)
	{
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM tbluser WHERE tbluser.userID=".$oid;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
		$userDomain = new TbluserDomain($res);	
		return $userDomain;
	}
	
	public function findByUnamePass($username, $password)
	{
		$userDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();		
		$inSQL = "SELECT * FROM tbluser WHERE tbluser.userName = '" . $username ."' AND tbluser.pass = '" . $password . "'"; 
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
		if($res != false)
			$userDomain = new TbluserDomain($res);
		return $userDomain;
	}
	
	//get lock out time
	public function getLockOutTimeByName($name)
	{
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT tbluser.lockOutTime FROM tbluser WHERE tbluser.userName='".$name."'";
		$res = $this->getMySQLConnection()->mysqlOneField($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $res;
	}
	
	//update lock out time
	public function updateLockOutTimeByName($name,$lockOutTime)
	{
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE tbluser SET tbluser.lockOutTime =".$lockOutTime." WHERE tbluser.userName='".$name."'";
		$res = $this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}
}
?>